﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DQYT.HTDZ.BLL;
using DQYT.HTDZ.Model;
using System.Data;

namespace DQYT.HTDZ.Web.Views.SystemManage
{
    public partial class User : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            #region
            try
            {
                lblMsg.Text = "";
                BDMenuBLL menuBLL = new BDMenuBLL();
                lblPath.Text = menuBLL.GetTitlePath(Request["mnu_id"]);
                if (!IsPostBack)
                {
                    if (Request["action"] == "add")
                    {
                        btnEdit.Visible = false;
                        InitData(false);
                    }
                    if (Request["action"] == "update")
                    {
                        btnAdd.Visible = false;
                        InitData(true);
                    }
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "错误信息：" + ex.Message;
            }
            #endregion
        }

        //添加
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            #region
            try
            {
                BDUserBLL dUser = new BDUserBLL();
                BDUserModel mUser = new BDUserModel();
                mUser.USE_CODE = txtUSE_CODE.Text.Trim();
                mUser.USE_NAME = txtUSE_NAME.Text.Trim();
                if (mUser.USE_CODE == string.Empty || mUser.USE_NAME == string.Empty)
                {
                    lblMsg.Text = "请填写用户代码和姓名！";
                    return;
                }
                else
                {
                    bool isCZ = dUser.CheckCode(mUser.USE_CODE);
                    if (isCZ)
                    {
                        lblMsg.Text = "用户代码已经存在！";
                        return;
                    }
                }
                string strBMMC = ddlBUM_MC.SelectedItem.Text.Trim();
                string strYHJS = ddlJSE_NAME.SelectedItem.Text.Trim();
                if (strBMMC == string.Empty || strYHJS == string.Empty)
                {
                    lblMsg.Text = "请选择部门和角色！";
                    return;
                }
                mUser.BUM_ID = Convert.ToInt32(ddlBUM_MC.SelectedItem.Value.Trim());
                mUser.JSE_ID = Convert.ToInt32(ddlJSE_NAME.SelectedItem.Value.Trim());
                mUser.USE_BH = txtUSE_BH.Text.Trim();
                mUser.USE_EMAIL = txtUSE_EMAIL.Text.Trim();
                mUser.USE_IPADRESS = txtUSE_IPADRESS.Text.Trim();
                mUser.USE_MACDRESS = txtUSE_MACDRESS.Text.Trim();
                mUser.USE_LASTTIME = "";
                mUser.USE_PASSWORD = "888888";
                mUser.USE_PHONE = txtUSE_PHONE.Text.Trim();
                if (ddlUSE_ZT.Text == "正常")
                {
                    mUser.USE_ZT = 1;
                }
                else
                {
                    mUser.USE_ZT = 0;
                }

                mUser.USE_BZ = txtUSE_BZ.Text.Trim();
                mUser.USE_ID = dUser.GetNextID();
                bool isOK = dUser.Add(mUser);
                if (isOK)
                {
                    Response.Redirect("UserManage.aspx");
                }
                else
                {
                    lblMsg.Text = "保存失败！";
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "错误信息：" + ex.Message;
            }
            #endregion
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="isEdit">是否是编辑</param>
        private void InitData(bool isEdit)
        {
            #region
            //初始化部门
            ddlBUM_MC.Items.Clear();
            BDDepartmentBLL dBuMen = new BDDepartmentBLL();
            DataTable dt = dBuMen.GetList("").Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListItem li = new ListItem(dt.Rows[i]["BUM_MC"].ToString(), dt.Rows[i]["BUM_ID"].ToString());
                ddlBUM_MC.Items.Add(li);
            }
            if (dt.Rows.Count > 0)
            {
                ddlBUM_MC.SelectedValue = "0";
            }
            //初始化角色
            ddlJSE_NAME.Items.Clear();
            BDRoleBLL dJiaoSe = new BDRoleBLL();
            DataTable dtJiaoSe = dJiaoSe.GetList("").Tables[0];
            for (int i = 0; i < dtJiaoSe.Rows.Count; i++)
            {
                ListItem li = new ListItem(dtJiaoSe.Rows[i]["JSE_NAME"].ToString(), dtJiaoSe.Rows[i]["JSE_ID"].ToString());
                ddlJSE_NAME.Items.Add(li);
            }
            if (dtJiaoSe.Rows.Count > 0)
            {
                ddlJSE_NAME.SelectedValue = "0";
            }
            //初始化状态
            ddlUSE_ZT.Items.Clear();
            ddlUSE_ZT.Items.Add("正常");
            ddlUSE_ZT.Items.Add("停用");
            ddlUSE_ZT.Text = "正常";
            //初始化基本信息
            if (isEdit)
            {
                BDUserBLL dUser = new BDUserBLL();
                BDUserModel mUser = new BDUserModel();
                int iUSE_ID = Convert.ToInt32(Request["id"].ToString());
                mUser = dUser.GetModel(iUSE_ID);
                txtUSE_BH.Text = mUser.USE_BH;
                txtUSE_BZ.Text = mUser.USE_BZ;
                txtUSE_CODE.Text = mUser.USE_CODE;
                txtUSE_EMAIL.Text = mUser.USE_EMAIL;
                txtUSE_IPADRESS.Text = mUser.USE_IPADRESS;
                txtUSE_LASTTIME.Text = mUser.USE_LASTTIME;
                txtUSE_MACDRESS.Text = mUser.USE_MACDRESS;
                txtUSE_NAME.Text = mUser.USE_NAME;
                txtUSE_PHONE.Text = mUser.USE_PHONE;
                ddlBUM_MC.SelectedValue = mUser.BUM_ID.ToString();
                ddlJSE_NAME.SelectedValue = mUser.JSE_ID.ToString();
                ddlUSE_ZT.Text = "停用";
                if (mUser.USE_ZT == 1)
                {
                    ddlUSE_ZT.Text = "正常";
                }

            }

            #endregion
        }

        //修改
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            #region
            try
            {
                string strUSE_ID = Request["id"].ToString();
                int iUSE_ID = Convert.ToInt32(strUSE_ID);
                BDUserBLL dUser = new BDUserBLL();
                BDUserModel mUser = new BDUserModel();
                mUser.USE_CODE = txtUSE_CODE.Text.Trim();
                mUser.USE_NAME = txtUSE_NAME.Text.Trim();
                if (mUser.USE_CODE == string.Empty || mUser.USE_NAME == string.Empty)
                {
                    lblMsg.Text = "请填写用户代码和姓名！";
                    return;
                }
                else
                {
                    bool isCZ = dUser.CheckCode(mUser.USE_CODE, strUSE_ID);
                    if (isCZ)
                    {
                        lblMsg.Text = "用户代码已经存在！";
                        return;
                    }
                }
                string strBMMC = ddlBUM_MC.SelectedItem.Text.Trim();
                string strYHJS = ddlJSE_NAME.SelectedItem.Text.Trim();
                if (strBMMC == string.Empty || strYHJS == string.Empty)
                {
                    lblMsg.Text = "请选择部门和角色！";
                    return;
                }
                BDUserModel mUser1 = new BDUserModel();
                mUser1 = dUser.GetModel(iUSE_ID);
                mUser.USE_ID = mUser1.USE_ID;
                mUser.BUM_ID = Convert.ToInt32(ddlBUM_MC.SelectedItem.Value.Trim());
                mUser.JSE_ID = Convert.ToInt32(ddlJSE_NAME.SelectedItem.Value.Trim());
                mUser.USE_BH = txtUSE_BH.Text.Trim();
                mUser.USE_EMAIL = txtUSE_EMAIL.Text.Trim();
                mUser.USE_IPADRESS = txtUSE_IPADRESS.Text.Trim();
                mUser.USE_MACDRESS = txtUSE_MACDRESS.Text.Trim();
                mUser.USE_LASTTIME = txtUSE_LASTTIME.Text.Trim();
                mUser.USE_PASSWORD = mUser1.USE_PASSWORD;
                mUser.USE_PHONE = txtUSE_PHONE.Text.Trim();
                if (ddlUSE_ZT.Text == "正常")
                {
                    mUser.USE_ZT = 1;
                }
                else
                {
                    mUser.USE_ZT = 0;
                }

                mUser.USE_BZ = txtUSE_BZ.Text.Trim();

                bool isOK = dUser.Update(mUser);
                if (isOK)
                {
                    Response.Redirect("UserManage.aspx");
                }
                else
                {
                    lblMsg.Text =  "保存失败！";
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "错误信息：" + ex.Message;
            }
            #endregion

        }
        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCZMM_Click(object sender, EventArgs e)
        {
            #region
            try
            {
                string strUSE_ID = Request["id"].ToString();
                int iUSE_ID = Convert.ToInt32(strUSE_ID);
                BDUserBLL dUser = new BDUserBLL();
                bool isOK = dUser.UpdatePwd(iUSE_ID, "888888");
                if (isOK)
                {
                    lblMsg.Text = "密码已经成功重置为：888888";
                }
                else
                {
                    lblMsg.Text = "更新失败！";
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "错误信息：" + ex.Message;
            }
            #endregion
        }
    }
}